//
//  AlbumsViewController.m
//  Puzzle_Crazy
//
//  Created by VIETHUNG on 4/2/14.
//  Copyright (c) 2014 DanielLe. All rights reserved.
//

#import "AlbumsViewController.h"
#import "CellObject.h"
#import "MyCell.h"
#import "DBManager.h"
#import "PuzzleObject.h"
#import "UIButton+Block.h"
#import "PuzzleViewController.h"

@interface AlbumsViewController ()

@end

@implementation AlbumsViewController
@synthesize arrData;
@synthesize idPu;

- (void)viewDidLoad
{
    [super viewDidLoad];
    arrData = [[NSMutableArray alloc] init];
    arrData = [[DBManager shareInstance] showDataPuzzle:idPu];
    // Remove table cell separator
    [myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    // Assign our own backgroud for the view
    myTableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    
    // Add padding to the top of the table view
    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
    myTableView.contentInset = inset;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [myTableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - DataSource UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  arrData.count;
}

- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rowCount = [self tableView:myTableView numberOfRowsInSection:0];
    NSInteger rowIndex = indexPath.row;
    UIImage *background = nil;
    
    if (rowIndex == 0) {
        background = [UIImage imageNamed:@"cell_top.png"];
    } else if (rowIndex == rowCount - 1) {
        background = [UIImage imageNamed:@"cell_bottom.png"];
    } else {
        background = [UIImage imageNamed:@"cell_middle.png"];
    }
    
    return background;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
//    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
//        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    PuzzleObject *puzzleObj = (PuzzleObject*)[arrData objectAtIndex:indexPath.row];
    cell.imgViewProduct.image = [UIImage imageNamed:puzzleObj.Objimage];
    [cell.btnMedium setEnabled:NO];
    [cell.btnHard setEnabled:NO];
    /* star easy */
    if ([puzzleObj.Objeasy intValue] >= k1StarEasy) {
        // 1 star
        cell.starEasy1.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k2StarEasy < [puzzleObj.Objeasy intValue] && [puzzleObj.Objeasy intValue] < k1StarEasy) {
        // 2 star
        cell.starEasy1.image = [UIImage imageNamed:@"star2.png"];
        cell.starEasy2.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k3StarEasy < [puzzleObj.Objeasy intValue] && [puzzleObj.Objeasy intValue] < k2StarEasy) {
        // 3 star
        cell.starEasy1.image = [UIImage imageNamed:@"star2.png"];
        cell.starEasy2.image = [UIImage imageNamed:@"star2.png"];
        cell.starEasy3.image = [UIImage imageNamed:@"star2.png"];
        [cell.btnEasy setEnabled:NO];
        [cell.btnMedium setEnabled:YES];
    }
    
    /* star medium */
    if ([puzzleObj.Objmedium intValue] >= k1StarEasy) {
        // 1 star
        cell.starMedium1.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k2StarEasy < [puzzleObj.Objmedium intValue] && [puzzleObj.Objmedium intValue] < k1StarEasy) {
        // 2 star
        cell.starMedium1.image = [UIImage imageNamed:@"star2.png"];
        cell.starMedium2.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k3StarEasy < [puzzleObj.Objmedium intValue] && [puzzleObj.Objmedium intValue] < k2StarEasy) {
        // 3 star
        cell.starMedium1.image = [UIImage imageNamed:@"star2.png"];
        cell.starMedium2.image = [UIImage imageNamed:@"star2.png"];
        cell.starMedium3.image = [UIImage imageNamed:@"star2.png"];
        [cell.btnMedium setEnabled:NO];
        [cell.btnHard setEnabled:YES];
    }
    
    /* star hard */
    if ([puzzleObj.Objhard intValue] >= k1StarEasy) {
        // 1 star
        cell.starHard1.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k2StarEasy < [puzzleObj.Objhard intValue] && [puzzleObj.Objhard intValue] < k1StarEasy) {
        // 2 star
        cell.starHard1.image = [UIImage imageNamed:@"star2.png"];
        cell.starHard2.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k3StarEasy < [puzzleObj.Objhard intValue] && [puzzleObj.Objhard intValue] < k2StarEasy) {
        // 3 star
        cell.starHard1.image = [UIImage imageNamed:@"star2.png"];
        cell.starHard2.image = [UIImage imageNamed:@"star2.png"];
        cell.starHard3.image = [UIImage imageNamed:@"star2.png"];
        [cell.btnHard setEnabled:NO];
    }
    
    [cell.btnEasy addAction:^(UIButton *btn) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        PuzzleViewController *puzzleView = (PuzzleViewController*)[storyboard instantiateViewControllerWithIdentifier:kPuzzleIP5];
        puzzleView.level = levelEasy;
        puzzleView.strImage = puzzleObj.Objimage;
        [puzzleView setModalPresentationStyle:UIModalPresentationCustom];
        puzzleView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:puzzleView animated:YES completion:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    [cell.btnMedium addAction:^(UIButton *btn) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        PuzzleViewController *puzzleView = (PuzzleViewController*)[storyboard instantiateViewControllerWithIdentifier:kPuzzleIP5];
        puzzleView.level = levelMedium;
        puzzleView.strImage = puzzleObj.Objimage;
        [puzzleView setModalPresentationStyle:UIModalPresentationCustom];
        puzzleView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:puzzleView animated:YES completion:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    [cell.btnHard addAction:^(UIButton *btn) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        PuzzleViewController *puzzleView = (PuzzleViewController*)[storyboard instantiateViewControllerWithIdentifier:@"PuzzleViewController"];
        puzzleView.level = levelHard;
        puzzleView.strImage = puzzleObj.Objimage;
        [puzzleView setModalPresentationStyle:UIModalPresentationCustom];
        puzzleView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:puzzleView animated:YES completion:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    
    // Asign our own background image for the cell
    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
    cellBackgroundView.image = background;
    cell.backgroundView = cellBackgroundView;
    
    return cell;
}

#pragma mark - Delegate UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)back:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
